<?php


namespace app\sonpartner\controller;


class BigData extends Common
{
    /**
     * 会员数据
     */
    public function membersData()
    {
        $id = $this -> id;
        $where['partner_id'] = $id;
        $input = input();

        //新增会员
        $late1 = isset($input['late1']) ? $input['late1'] : 30;
        $user_new = db('user_manage')
            ->where($where)
            ->where('DATE_SUB(CURDATE(),INTERVAL '.$late1.' DAY)<time')
            ->field("count(id) as total_num,DATE_FORMAT(time,'%Y-%m-%d') as shijian")
            ->group('shijian')
            ->select();
        $data['user_new'] = $user_new;

        //会员属性分析
        //会员等级
        $user_level = db('user_manage')
            ->where($where)
            ->field('is_vip,count(id) as total_num')
            ->group('is_vip')
            ->select();
        foreach ($user_level as $key => $value) {
            if($value['is_vip'] == 1) {
                $user_level[$key]['is_vip'] = 'VIP';
            } else {
                $user_level[$key]['is_vip'] = '普通';
            }
        }
        $data['user_level'] = $user_level;
        //会员性别
        $user_sex = db('user_manage')
            ->where($where)
            ->field('sex,count(id) as total_num')
            ->group('sex')
            ->select();
        foreach ($user_sex as $key => $value) {
            if($value['sex'] == 1) {
                $user_sex[$key]['sex'] = '男';
            } else if($value['sex'] == 2) {
                $user_sex[$key]['sex'] = '女';
            } else {
                $user_sex[$key]['sex'] = '未知';
            }
        }
        $data['user_sex'] = $user_sex;
        //会员职业
        $user_pro = db('user_manage')
            ->where($where)
            ->field('zhiye,count(id) as total_num')
            ->group('zhiye')
            ->select();
        $data['user_pro'] = $user_pro;

        //会员所在地分布
        //省份分布
        $user_province = db('user_manage')
            ->where($where)
            ->field('province,count(id) as total_num')
            ->group('province')
            ->select();
        $data['user_province'] = $user_province;
        //城市分布
        $province = isset($input['province']) ? $input['province'] : '';
        if(!empty($province)) {
            $where_p['province'] = $province;
        } else {
            $where_p = [];
        }
        $user_city = db('user_manage')
            ->where($where)
            ->where($where_p)
            ->field('city,count(id) as total_num')
            ->group('city')
            ->select();
        $data['user_city'] = $user_city;

        returnSuccess($data);
    }
}